39c373
@@ -403,8 +403,9 @@
public class CachingConnectionFactory extends AbstractConnectionFactory
 	}
 
 	private Channel getChannel(ChannelCachingConnectionProxy connection, boolean transactional) {
+		Semaphore checkoutPermits = null;
 		if (this.channelCheckoutTimeout > 0) {
-			Semaphore checkoutPermits = this.checkoutPermits.get(connection);
+			checkoutPermits = this.checkoutPermits.get(connection);
 			if (checkoutPermits != null) {
 				try {
 					if (!checkoutPermits.tryAcquire(this.channelCheckoutTimeout, TimeUnit.MILLISECONDS)) {
@@ -484,7 +485,19 @@
public class CachingConnectionFactory extends AbstractConnectionFactory
 			}
 		}
 		if (channel == null) {
-			channel = getCachedChannelProxy(connection, channelList, transactional);
+			try {
+				channel = getCachedChannelProxy(connection, channelList, transactional);
+			}
+			catch (RuntimeException e) {
+				if (checkoutPermits != null) {
+					checkoutPermits.release();
+					if (logger.isDebugEnabled()) {
+						logger.debug("Could not get channel; released permit for " + connection + ", remaining:"
+							+ checkoutPermits.availablePermits());
+					}
+				}
+				throw e;
+			}
 		}
 		return channel;
 	}
@@ -956,7 +969,7 @@
public class CachingConnectionFactory extends AbstractConnectionFactory
 				if (checkoutPermits != null) {
 					checkoutPermits.release();
 					if (logger.isDebugEnabled()) {
-						logger.debug("Released permit for " + this.theConnection + ", remaining:"
+						logger.debug("Released permit for '" + this.theConnection + "', remaining: "
 							+ checkoutPermits.availablePermits());
 					}
 				}
